package View;

import Service.DormService;
import Service.ServiceImpl.UserServiceImpl;
import Service.UserService;
import View.CheckCode.CreateCheckCode;
import View.Index.Index_admin;
import View.Index.Index_user;
import domain.User;
import org.w3c.dom.traversal.NodeIterator;

import javax.imageio.stream.FileImageInputStream;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.List;
import java.util.Locale;

public class Login extends JFrame {
    public static void main(String[] args) {
        new Login().setVisible(true);
    }

    public Login(){
        init();
        componentInit();
    }
    String checkcode=null;
    public void init(){
        this.setTitle("登录");
        this.setSize(500,400);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(){
        //------------------JLabel--------------------
        JLabel label1= new JLabel();
        label1.setText("宿舍管理系统");
        label1.setBounds(187,17,200,100);
        JLabel label2= new JLabel();
        label2.setText("用户名:");
        label2.setBounds(96,116,78,38);
        JLabel label3= new JLabel();
        label3.setText("密码:");
        label3.setBounds(96,168,78,38);
        JLabel label4= new JLabel();
        CreateCheckCode createCheckCode = new CreateCheckCode();
        checkcode = createCheckCode.DrawCheckcode();
        Icon icon = new ImageIcon("checkcode.jpg");
        label4.setIcon(icon);
        System.out.println(checkcode);
        label4.setBounds(96,220,78,38);

        //------------------JTextField--------------------
        JTextField field1= new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(184,116,193,31);
        JTextField field2= new JTextField();
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(184,220,193,31);
        //------------------JPasswordField--------------------
        JPasswordField password1= new JPasswordField();
        password1.setBorder(new LineBorder(Color.lightGray));
        password1.setBounds(184,168,191,35);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("登录");
        button1.setBounds(101,314,95,27);
        JButton button2= new JButton();
        button2.setText("退出");
        button2.setBounds(297,314,95,27);
        JButton button3= new JButton();
        button3.setText("注册");
        button3.setBounds(297,270,95,27);




        //------------------JComboBox--------------------
        String[] item = new String[3];
        item[0]="请选择用户类型:";
        item[1]="admin";
        item[2]="user";
        JComboBox comboBox1= new JComboBox(item);
        comboBox1.setBounds(101,271,120,27);

        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(field1);
        this.add(field2);
        this.add(button1);
        this.add(button2);
        this.add(button3);
        this.add(password1);
        this.add(comboBox1);


        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String usertype = (String) comboBox1.getSelectedItem();
                String username = field1.getText();
                String password = password1.getText();
                String ckcode = field2.getText();
                UserService service = new UserServiceImpl();
                List<User> userList = service.findUserByUsername(username);

                User user1 = userList.get(0);

                if(ckcode.toLowerCase(Locale.ROOT).equals(checkcode.toLowerCase(Locale.ROOT))) {
                    if (usertype.equals(user1.getType()) && username.equals(user1.getUsername()) && password.equals(user1.getPassword())) {
                        if ("user".equals(user1.getType())) {
                            dispose();
                            new Index_user(user1).setVisible(true);
                        }
                        if ("admin".equals(user1.getType())) {
                            dispose();
                            new Index_admin(user1).setVisible(true);
                        }
                    } else {
                        JOptionPane.showMessageDialog(new JFrame(),"登陆失败用户名或密码错误");
                        field1.setText("");
                        field2.setText("");
                        comboBox1.setSelectedIndex(0);
                    }
                }else {
                    JOptionPane.showMessageDialog(new JFrame(),"验证码错误请重新输入");
                    field2.setText("");
                    checkcode = createCheckCode.DrawCheckcode();

                    byte[] date = image2byte("checkcode.jpg");

                    Icon icon = new ImageIcon(date);
                    label4.setIcon(icon);
                    System.out.println(checkcode);
                }
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new regist().setVisible(true);
            }
        });

        label4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                checkcode = createCheckCode.DrawCheckcode();

                byte[] date = image2byte("checkcode.jpg");

                Icon icon = new ImageIcon(date);
                label4.setIcon(icon);
                System.out.println(checkcode);

            }
        });
    }

    public byte[] image2byte(String path){
        byte[] data = null;
        FileImageInputStream input = null;
        try {
            input = new FileImageInputStream(new File("checkcode.jpg"));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int numBytesRead = 0;
            while ((numBytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, numBytesRead);
            }
            data = output.toByteArray();
            output.close();
            input.close();
        } catch (FileNotFoundException ex1) {
            ex1.printStackTrace();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        }
        return data;
    }

}
